package com.zz.controller;


import com.zz.entity.Book;
import com.zz.exception.DataValidationException;
import com.zz.form.BookForm;
import com.zz.service.BookService;
import com.zz.util.ResultVOUtil;
import com.zz.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 单词本
 * @Author: Bsea
 * @CreateDate: 2019/5/20$ 21:06$
 */
@RequestMapping("/book")
@Slf4j
@RestController
public class BookController {
    @Resource
    BookService bookService;
    @PostMapping("add")
    public ResultVO<Map<String, String>> add(@RequestBody  @Valid BookForm bookForm, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            log.error("参数不正确, BookForm={}", bookForm);
            throw new DataValidationException(bindingResult.getFieldError().getDefaultMessage());
        }
        System.out.println(bookForm);
        Book book=bookService.add(bookForm);
        Map<String, String> map=new HashMap();
        map.put("id",book.getId()+"");
        return ResultVOUtil.success(map);
    }

    @GetMapping("/findByOpenid/{openid}")
    public ResultVO<List<Book>> getByOpenId(@PathVariable("openid") String openid){
       return ResultVOUtil.success(bookService.findByOpenid(openid));
    }
}
